package github.pop.ares.network;

import github.pop.ares.group.ModGroup;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.world.World;
import net.minecraftforge.fml.network.PacketDispatcher;
import net.minecraftforge.fml.network.PacketDistributor;

/**
 * create a item can send message
 */
public class ObsidianMessage extends Item {


    public ObsidianMessage() {
        super(new Properties().tab(ModGroup.itemGroup));
    }

    @Override
    public ActionResult<ItemStack> use(World worldIn, PlayerEntity playerIn, Hand handIn) {
        // when right click will trigger method
        boolean isClientSide = worldIn.isClientSide;
        if(isClientSide){
            // client send to server
            Networking.INSTANCE.sendToServer(new SendPack("From the Client"));
        }

        if(!isClientSide){
            // server send to target player
            // server send to client
            // because server has many connection player
            // so you need confirm send target

//            PacketDistributor. has many send type
            // this use player type
            Networking.INSTANCE.send(
                    PacketDistributor.PLAYER.with(()-> (ServerPlayerEntity) playerIn),
                    new SendPack("From Server")
                    );
        }

        return super.use(worldIn, playerIn, handIn);
    }
}
